//
//  AddPhotoController.m
//  iGSS
//
//  Copyright 2010 Electronic Business Systems Ltd.
//
//  Licensed under the Apache License, Version 2.0 (the "License");
//  you may not use this file except in compliance with the License.
//  You may obtain a copy of the License at
//
//  http://www.apache.org/licenses/LICENSE-2.0
//
//  Unless required by applicable law or agreed to in writing, software
//  distributed under the License is distributed on an "AS IS" BASIS,
//  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
//  See the License for the specific language governing permissions and
//  limitations under the License.
//

#import "AddPhotoController.h"

#import "NSString+GSSAdditions.h"
#import "FormattingUtilities.h"
#import "GSSCommunicator.h"

@implementation AddPhotoController

@synthesize folder;

@synthesize navItem;
@synthesize nameTextField;
@synthesize backgroundView;
@synthesize imageView;

@synthesize saveButton;
@synthesize chooseImageButton;
@synthesize takePhotoButton;

- (id)initWithFolder:(GSSObject *)aFolder {
    if (self = [super initWithNibName:@"AddPhotoController" bundle:nil]) {
        folder = [aFolder retain];
    }
    return self;
}

- (void)dealloc {
    [folder release];
    
    [navItem release];
    [nameTextField release];
    [backgroundView release];
    [imageView release];
    
    [saveButton release];
    [chooseImageButton release];
    [takePhotoButton release];
    
    [super dealloc];
}


//MARK: -
//MARK: UIViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.navItem.title = LocalizedString(@"Add image");
    NSString *name = [[GSSCommunicator defaultCommunicator] generateUniqueName:@"untitled image.jpg"
                                                                  withTemplate:@"untitled image %d.jpg"
                                                                     forObject:self.folder];
    self.nameTextField.text = [name stringByDeletingPathExtension];
    
    self.chooseImageButton.title = LocalizedString(@"Choose image");
    self.chooseImageButton.enabled = [UIImagePickerController
                                      isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary];
    self.takePhotoButton.title = LocalizedString(@"Take photo");
    self.takePhotoButton.enabled = [UIImagePickerController isSourceTypeAvailable:
                                    UIImagePickerControllerSourceTypeCamera];
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    return YES;
}


//MARK: -
//MARK: UITextFieldDelegate

- (BOOL)textFieldShouldReturn:(UITextField *)textField {
    [textField resignFirstResponder];
    return YES;
}


//MARK: -
//MARK: UIImagePickerControllerDelegate

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
	[self dismissModalViewControllerAnimated:YES];
    
    UIImage *image = [info objectForKey:@"UIImagePickerControllerOriginalImage"];
    if (image) {
        self.imageView.image = image;
        
        UIImage *white = [UIImage imageNamed:@"white.png"];
        UIGraphicsBeginImageContext(image.size);
        [white drawInRect:CGRectMake(0, 0, image.size.width, image.size.height)];
        self.backgroundView.image = UIGraphicsGetImageFromCurrentImageContext();    
        UIGraphicsEndImageContext();
        
        self.nameTextField.hidden = NO;
        self.saveButton.enabled = YES;
    }
}

//MARK: -
//MARK: IBActions

- (IBAction)save {
    NSString *baseName = self.nameTextField.text;
    if ([baseName length] > 0) {
        GSSCommunicator *communicator = [GSSCommunicator defaultCommunicator];
        NSString *name = [baseName stringByAppendingPathExtension:@"jpg"];
        NSString *uri = [folder.uri stringByAppendingURIComponent:name];    
        GSSObject *file = [communicator objectWithURI:uri withType:GSSObjectTypeFile];
        NSData *data = UIImageJPEGRepresentation(self.imageView.image, 1.0);
        file.name = name;
        file.content = @"image/jpeg";
        [communicator createFile:file withData:data inFolder:folder];
    }
    
    [self dismissModalViewControllerAnimated:YES];
}

- (IBAction)cancel {
    [self dismissModalViewControllerAnimated:YES];
}

- (IBAction)chooseImage {
    UIImagePickerController *imagePickerController = [[UIImagePickerController alloc] init];
    imagePickerController.delegate = self;
    imagePickerController.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
    [self presentModalViewController:imagePickerController animated:YES];
	[imagePickerController release];
}

- (IBAction)takePhoto {
    UIImagePickerController *imagePickerController = [[UIImagePickerController alloc] init];
    imagePickerController.delegate = self;
    imagePickerController.sourceType = UIImagePickerControllerSourceTypeCamera;
    [self presentModalViewController:imagePickerController animated:YES];
	[imagePickerController release];    
}

@end
